package hk.hku.hooku.servlet;

import hk.hku.hooku.db.ContactDB;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AutoCompleteServlet
 */
public class AutoCompleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutoCompleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url=request.getParameter("url");
		if(url!=null){
			String name=request.getParameter("name");
			String email = request.getParameter("email");
			if(name != null)
			{
				String returnemail = ContactDB.getContactByHint(name,false);
				PrintWriter writer=response.getWriter();
				if(returnemail != null){
					writer.write(returnemail);
				}else{
					writer.write("-");
				}
			}
			else if(email != null)
			{
				String returnname = ContactDB.getContactByHint(email,true);
				PrintWriter writer=response.getWriter();
				if(returnname != null){
					writer.write(returnname);
				}else{
					writer.write("-");
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
